package com.jxj.controller;

import com.alibaba.fastjson.JSONObject;
import com.jxj.dao.*;
import com.jxj.entity.*;
import com.jxj.entity.gen.TOrganizationMsg;
import com.jxj.service.AddResumeService;
import com.jxj.service.OrganizationService;
import com.jxj.util.GetSocMapping;
import com.jxj.util.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
public class ResumeMsgController {
    @Resource
    private AddResumeService addResumeService;
    @Resource
    private ResumeMsgMapper resumeMsgMapper;
    @Resource
    private OrgzMapper orgzMapper;
    @Resource
    private DepMapper depMapper;
    @Resource
    private RecruitMsgMapper recruitMsgMapper;
    @Autowired
    OrganizationService organizationService;

    static String telReg = "1[358]\\d{9}";
    static String qqRegex = "[1-9][0-9]{4,14}";
    static String ip = "https://hd.jxj777.top";

    @PostMapping("/organization/resume")
    public Resp addResumeMsg(ResumeMsg resumeMsg,
                             @RequestParam("organization") String organization,
                             @RequestParam("department") String department,
                             @RequestParam("photo") MultipartFile photo,
                             @RequestParam("sex") String sex,
                             @RequestParam("grade") String grade

    ) throws IOException {
        if (resumeMsg.getPhone() != null && !resumeMsg.getPhone().matches(telReg))
            return new Resp(HttpStatus.BAD_REQUEST, "手机号错误", null);
        if (resumeMsg.getQq() != null && !resumeMsg.getQq().matches(qqRegex))
            return new Resp(HttpStatus.BAD_REQUEST, "qq号错误", null);

        if (photo != null) {
            String originalFilename = photo.getOriginalFilename();
            /**
             * @TODO 路径正式部署时改回来
             */
            /*String classespath = "/usr/img/" + originalFilename;*/
            String classespath = "D:\\temp\\" + originalFilename;
            photo.transferTo(new File(classespath));
            /*File dist = new File(classespath);
            photo.transferTo(dist);*/
            /*String filePath = ip + "/usr/img/" + originalFilename;*/
            String filePath = "D:\\temp\\" + originalFilename;
            resumeMsg.setPhotoPath(filePath);
            log.info(filePath);
        }
        if(sex.equals("男"))
        resumeMsg.setSexx(true);
        else
            resumeMsg.setSexx(false);

        Integer aGrade = 0;
        switch (grade.charAt(1)) {
            case '四':
                aGrade++;
            case '三':
                aGrade++;
            case '二':
                aGrade++;
            case '一':
                aGrade++;
        }

        resumeMsg.setAGrade(aGrade);

        resumeMsg.setDId(depMapper.selectDId(department));

        int oId = orgzMapper.selectOId(organization);
        resumeMsg.setOId(oId);

        orgzMapper.updateHot(oId);

        Date nowTime = new Date();
        Timestamp timestamp = new Timestamp(nowTime.getTime());
        resumeMsg.setAddTime(timestamp);
        resumeMsg.setUid(1);

        log.info(resumeMsg.toString());


        int flag = addResumeService.addResumeMsg(resumeMsg);
        if (flag > 0) return new Resp(HttpStatus.OK, "上传成功", null);
        else return new Resp(HttpStatus.OK, "上传失败", null);
    }

    @GetMapping("/stuinfors")
    public String selectBasicStuMsg(@RequestHeader("Authorization") String token , String class_one){

        Integer id = JWTUtil.getOrgId(token.split(" ")[1]);

        log.info("\n------\norgid{}\n------\n" , JWTUtil.getOrgId(token.split(" ")[1]));
        log.info("\n------\ntoken{}\n------\n" , token);

        JSONObject jsonObject = new JSONObject();

        jsonObject.put("code" , 200);
        jsonObject.put("msg" , "查询成功");

        if (class_one.equals("组织")) {
            int oId = recruitMsgMapper.selectOId(id);
            List<BasicStuMsgVO> basicStuMsgVOS = resumeMsgMapper.selectOrgBasicStuMsg(oId);
            jsonObject.put("data" , basicStuMsgVOS);
        } else {
            Map<Integer, String> map = GetSocMapping.getMap();
            List<SocResumeVO> resumeVOS = resumeMsgMapper.selectSocBasicStuMsg(id);

            jsonObject.put("data" , resumeVOS);
        }

        return jsonObject.toJSONString();
    }

    @GetMapping("/getstuinfor")
    public Resp<StuRecruitMsgVO> selectStuMsgById(int id){
        StuRecruitMsgVO stuRecruitMsgVO = resumeMsgMapper.selectStuMsg(id);
        return new Resp<StuRecruitMsgVO>(HttpStatus.OK, "查询成功", stuRecruitMsgVO);
    }
}
